package com.weizhi.system.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.weizhi.server.dto.RoleDto;
import com.weizhi.server.dto.ResponseDto;
import com.weizhi.server.service.RoleService;
import com.weizhi.server.util.JsonUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("admin/role")
public class RoleController {

    private final static Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService roleService;

    @RequestMapping("list")
    public ResponseDto list(HttpServletRequest req) {
        JSONObject param = JsonUtil.convertRequest(req);
        logger.info("roleController-list, param={}", param);
        PageInfo pageInfo = roleService.list(param);
        return ResponseDto.createBySuccess(pageInfo);
    }

    @RequestMapping("all")
    public ResponseDto list() {
        logger.info("roleController-all");
        List<RoleDto> roleDtoList = roleService.all();
        return ResponseDto.createBySuccess(roleDtoList);
    }


    @RequestMapping("save")
    public ResponseDto save(@RequestBody RoleDto role) {
        logger.info("roleController-save, role={}", role);

        if(StringUtils.isBlank(role.getName()))
            throw new RuntimeException("请求参数异常");
        roleService.save(role);
        return ResponseDto.createBySuccess();
    }

    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable Long id) {
        logger.info("roleController-delete, id={}", id);
        roleService.delete(id);
        return ResponseDto.createBySuccess();
    }
}

